package com.xunge.controller;

import com.xunge.anno.Log;
import com.xunge.pojo.PageResult;
import com.xunge.pojo.Result;
import com.xunge.pojo.Student;
import com.xunge.pojo.StudentQueryParam;
import com.xunge.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    // 分页查询
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("分页请求参数:{}", studentQueryParam);
        PageResult<Student> p = studentService.page(studentQueryParam);
        return Result.success(p);
    }
    // 新增学生信息
    @Log
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("新增学生信息:{}", student);
        studentService.save(student);
        return Result.success();
    }
    // 根据id查询学生信息
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据id查询学生信息:{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }
    // 修改学生信息
    @Log
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学生信息:{}", student);
        studentService.update(student);
        return Result.success();
    }
    //根据id批量删除学生信息
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("根据ID批量删除学生信息:{}", ids);
        studentService.deleteById(ids);
        return Result.success();
    }
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("根据ID修改学生违纪次数:{}", id);
        studentService.updateViolation(id, score);
        return Result.success();
    }
}
